﻿using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCLD.Helpers;
using MVCLD.Models;
using System.Globalization;

namespace MVCLD.Controllers
{
    public class ArticleController : ApplicationController
    {

        #region Data Access
        ArticleModels am = new ArticleModels();
        MVCLDServiceModels db = new MVCLDServiceModels();
        #endregion

        //
        // GET: /Article/
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Article/Details/5
        public ActionResult Details(int URLID)
        {
            var theart = db.GetSingleArticle(URLID);
            theart = am.formatArticle(theart);
            return View(theart);
        }

        //
        // GET: /Article/Create
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Article/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Article/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Article/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Article/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Article/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: /article/submit?ID=10
        public ActionResult submit(int? id)
        {
            CategoryModels c = new CategoryModels();
            ViewData["CategoryDropDown"] = c.GetSubCategories(db.GetAllMainCategories().ToList());
            ViewData["ArticleTypes"] = db.GetAllArticleTypes().ToList();
            var ri = new RegionInfo(System.Threading.Thread.CurrentThread.CurrentUICulture.LCID);
            ViewData["Cost"] = ri.ISOCurrencySymbol;
            return View();
        }

        [HttpPost]
        public ActionResult submit(Article web, int? id)
        {
            // need to set IP address
            web.SubmitterIP = HttpContext.Request.UserHostAddress.ToString();

            // Check snippet length
            if (Helpers.Helpers.CountWordsInString(web.ArticleSnippet) > Settings.Settings.ArticleSnippetWordLength.ToInt32())
                ModelState.AddModelError("ArticleSnippet", "Snippet is too long");

            // Check snippet length
            if (Helpers.Helpers.CountWordsInString(web.ArticleSnippet) < Settings.Settings.ArticleMinWordLength.ToInt32())
                ModelState.AddModelError("ArticleSnippet", "Snippet is too short");

            // Check article title doesn't already exist
            if (am.DoesArticleNameExist(web.ArticleTitle))
                ModelState.AddModelError("ArticleTitle", "This article title already exists");

            if (!ModelState.IsValid)
            {
                CategoryModels c = new CategoryModels();
                ViewData["CategoryDropDown"] = c.GetSubCategories(db.GetAllMainCategories().ToList());
                ViewData["ArticleTypes"] = db.GetAllArticleTypes().ToList();
                return View();
            }
            else
            {
                db.InsertArticle(web);
                // get the cost of the submission
                var type = db.GetSingleArticleType(web.ArticleTypeID);
                if(type.Cost > 0)
                {
                    var ri = new RegionInfo(System.Threading.Thread.CurrentThread.CurrentUICulture.LCID);
                    Session["Formcode"] = am.PayPalFormCode(web, type, ri.ISOCurrencySymbol);
                    return RedirectToAction("Payment");
                }
                else
                {
                    return View("Submitted");
                }
            }
        }

        public ActionResult Submitted()
        {
            return View();
        }

        public ActionResult Payment()
        {
            if (Session["Formcode"] != null)
            {
                ViewData["FormCode"] = Session["Formcode"];
                return View();
            }
            else
            {
                return RedirectToAction("Error");
            }
        }
    }
}
